//
//  NYMyStarRankingCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/18.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyStarRankingCell.h"
@interface NYMyStarRankingCell ()
@property(nonatomic, strong) UIImageView *iconImgView;
@property(nonatomic, strong) TTTAttributedLabel *indexLabel;
@property(nonatomic, strong) TTTAttributedLabel *nameLabel;
@property(nonatomic, strong) TTTAttributedLabel *levelDesLabel;
/// 星星个数
@property(nonatomic, strong) TTTAttributedLabel *starCountLabel;
/// 答题次数
@property(nonatomic, strong) TTTAttributedLabel *answerCountLabel;
/// 总答题数
@property(nonatomic, strong) TTTAttributedLabel *answerTotalCountLabel;
/// 总答对数
@property(nonatomic, strong) TTTAttributedLabel *answerTotalRightCountLabel;
@end
@implementation NYMyStarRankingCell
+ (CGFloat)heightForObject:(NYMyRankingModel *)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
        return __WGHeight(120);
}
- (BOOL)shouldUpdateCellWithObject:(id)object atIndexPath:(NSIndexPath *)indexPath
{
        self.indexPath = indexPath;
        self.rankingModel = object;
        return YES;
}
- (void)setRankingModel:(NYMyRankingModel *)rankingModel
{
        _rankingModel = rankingModel;
        [self updateContent];
}
- (void)updateContent
{
        UIImage *iconImg = [UIImage imageNamed:[NSString stringWithFormat:@"ranking_%d",(int)self.indexPath.row + 1]];
        self.indexLabel.text = nil;
        
        if (!iconImg) {
                iconImg = [UIImage qmui_imageWithColor:UIColorHex(0x5396F7) size:CGSizeMake(__WGWidth(30), __WGWidth(30)) cornerRadius:__WGWidth(15)];
                self.indexLabel.text = self.rankingModel.ranking;
        }
        
        self.iconImgView.image = iconImg;
        self.nameLabel.text = self.rankingModel.username;
        NSAttributedString *attr = [[NSAttributedString alloc] initWithHTMLData:[NOT_NULL_STRING_DEFAULT_EMPTY(self.rankingModel.level) dataUsingEncoding:NSUnicodeStringEncoding] documentAttributes:nil];
        self.levelDesLabel.text = attr.string;
        self.starCountLabel.text = self.rankingModel.integral;
        self.answerCountLabel.text = self.rankingModel.answers_num;
        self.answerTotalCountLabel.text = self.rankingModel.total_answers_num;
        self.answerTotalRightCountLabel.text = self.rankingModel.correct_answers_num;
        
}
- (void)setupView
{
        [super setupView];
        UIView *contentView = [[UIView alloc] init];
        contentView.layer.cornerRadius = 8;
        contentView.clipsToBounds = YES;
        contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = UIColorForBackground;
        [self.contentView addSubview:contentView];
        
        [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(__WGHeight(15), __WGWidth(15), __WGHeight(0), __WGWidth(15))]);
        }];
        
        self.iconImgView = [[UIImageView alloc] init];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        self.indexLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        self.indexLabel.font = WGSystem15Font;
        self.indexLabel.textColor = [UIColor whiteColor];
        
        //*******
        self.nameLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0x333333) labelFont:WGSystem15Font];
        self.nameLabel.textAlignment = NSTextAlignmentLeft;
        self.levelDesLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0xFCB000) labelFont:WGSystem13Font];
        self.levelDesLabel.textAlignment = NSTextAlignmentLeft;
        
        UIStackView *nameLevelView = [self createStackView:@[self.nameLabel,self.levelDesLabel] withSpace:__WGWidth(13)];
        
        //*******
        self.starCountLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0x333333) labelFont:WGSystem12Font];
        TTTAttributedLabel *tmpLabel = [self createNormalLabelWithText:@"星星" labelColor:UIColorHex(0x999999) labelFont:WGSystem12Font];
        UIStackView *starCountView = [self createStackView:@[tmpLabel,self.starCountLabel] withSpace:__WGWidth(8)];
        
        self.answerCountLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0x333333) labelFont:WGSystem12Font];
        tmpLabel = [self createNormalLabelWithText:@"答题次数" labelColor:UIColorHex(0x999999) labelFont:WGSystem12Font];
        UIStackView *answerCountView = [self createStackView:@[tmpLabel,self.answerCountLabel] withSpace:__WGWidth(8)];
        
        UIStackView *rightTopView = [self createStackView:@[starCountView,answerCountView] withSpace:__WGWidth(40)];
        rightTopView.axis = UILayoutConstraintAxisHorizontal;
        
        //*******
        self.answerTotalCountLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0x333333) labelFont:WGSystem12Font];
        tmpLabel = [self createNormalLabelWithText:@"总答题数" labelColor:UIColorHex(0x999999) labelFont:WGSystem12Font];
        UIStackView *answerTotalCountView = [self createStackView:@[tmpLabel,self.answerTotalCountLabel] withSpace:__WGWidth(8)];
        
        self.answerTotalRightCountLabel = [self createNormalLabelWithText:nil labelColor:UIColorHex(0x333333) labelFont:WGSystem12Font];
        tmpLabel = [self createNormalLabelWithText:@"总答对数" labelColor:UIColorHex(0x999999) labelFont:WGSystem12Font];
        UIStackView *answerTotalRightCountView = [self createStackView:@[tmpLabel,self.answerTotalRightCountLabel] withSpace:__WGWidth(8)];
        
        UIStackView *rightBootomView = [self createStackView:@[answerTotalCountView,answerTotalRightCountView] withSpace:__WGWidth(40)];
        rightBootomView.axis = UILayoutConstraintAxisHorizontal;
        
        //****
        UIStackView *rightView = [self createStackView:@[rightTopView,rightBootomView] withSpace:__WGHeight(15)];
        [contentView addSubview:self.iconImgView];
        [contentView addSubview:self.indexLabel];
        [contentView addSubview:nameLevelView];
        [contentView addSubview:rightView];
        
        [self.iconImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(contentView);
                make.left.equalTo(contentView).offset(__WGWidth(15));
                make.width.mas_equalTo(__WGWidth(30));
        }];
        [self.indexLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.iconImgView);
        }];
        
        [nameLevelView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(contentView);
                make.left.equalTo(self.iconImgView.mas_right).offset(__WGWidth(15));
        }];
        
        [rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(contentView);
                make.left.equalTo(nameLevelView.mas_right).offset(__WGWidth(31));
                make.right.equalTo(contentView).offset(-__WGWidth(10));
        }];
        
}
- (UIStackView *)createStackView:(NSArray <__kindof UIView *>*)views withSpace:(CGFloat)space
{
        UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:views];
        stackView.axis = UILayoutConstraintAxisVertical;
        stackView.distribution = UIStackViewDistributionFillEqually;
        stackView.alignment = UIStackViewAlignmentFill;
        stackView.spacing = space;
        return stackView;
}
- (TTTAttributedLabel *)createNormalLabelWithText:(NSString *)text labelColor:(UIColor *)color labelFont:(UIFont *)font
{
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        label.font = font;
        label.textColor = color;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = text;
        return label;
}

@end
